package com.bw.wxy.factroy;


import java.util.concurrent.TimeUnit;

import com.bw.wxy.common.Api;
import com.bw.wxy.factroy.interceptor.*;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class TokenRetrofit implements HttpImpl{
    private TokenRetrofit(){}
    private static Retrofit retrofit;

    @Override
    public Retrofit getRetrofit() {
        return retrofit;
    }

    public static class Build{
        public Build(){
            createRetrofit();
        }

        private void createRetrofit() {
            HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            OkHttpClient.Builder client = new OkHttpClient.Builder();
            client.addInterceptor(new TokenInterceptor());
            client.addInterceptor(new SignInterceptor());
            client.addInterceptor(new ResponsInterceptor());
            client.addInterceptor(loggingInterceptor);
            client.readTimeout(60*1000, TimeUnit.MILLISECONDS);
            client.writeTimeout(60*1000,TimeUnit.MILLISECONDS);
            client.connectTimeout(60*1000,TimeUnit.MILLISECONDS);
            Retrofit.Builder builder = new Retrofit.Builder();
            builder.baseUrl(Api.BaseUrl);
            builder.client(client.build());
            builder.addConverterFactory(GsonConverterFactory.create());
            builder.addCallAdapterFactory(RxJava2CallAdapterFactory.create());
            retrofit=builder.build();
        }

        public TokenRetrofit build(){
            return new TokenRetrofit();
        }
    }
}

